<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 :width="isCollapse 根据是否折叠给出相应宽度 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 
          :unique-opened="true"  只展开一个菜单
          :collapse="isCollapse"  不折叠
          :collapse-transition="false" 取消折叠动画
          :router="true"  开启路由
          :default-active="activePath"  当前激活菜单的 index(点击时高亮)
         -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false" 
          :router="true"
          :default-active="activePath"
        >
        <!-- 一级菜单 -->
          <el-submenu :index="i+'' " v-for="(item,i) in menuList" :key="i">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path " v-for="(subItem,i) in item.children" 
            :key="i" @click="saveNavState('/'+subItem.path)">
               <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList:[],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,// 是否折叠
      activePath:''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = sessionStorage.getItem("activePath")
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    getMenuList(){
      this.$http.get('menus').then(res=>{
        if(res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg);
        this.menuList = res.data.data;
      })
    },
    // 点击按钮实现左侧边栏的显示和隐藏
    toggleCollapse(){
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接状态，用于点击菜单时的高亮激活
    saveNavState(activePath){
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
};
</script>

<style lang="scss" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}

.iconfont{
  margin-right: 10px;
}

.toggle-button{
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;// 鼠标变手
}
</style>